import time
from collections import deque

class queue_status:
    STATUS_RUNNING = 0
    STATUS_REJECTING_CONSUMERS = 1
    STATUS_REJECTING_PRODUCERS = 2
    STATUS_REJECTING_ALL = 3

    def __init__(self, status=STATUS_RUNNING):
        self.status = status
        self.consumers = deque()
        self.msgIn = 0
        self.msgOut = 0

    def getStatsDict(self, startTime):
        currTime = time.time()
        if startTime == currTime:
            currTime = startTime + 0.1
        timeDiff = currTime - startTime

        return { "status" : self.status,
                 "numWaitingConsumers" : len(self.consumers),
                 "messagesIn" : self.msgIn,
                 "messagesOut" : self.msgOut,
                 "incomingRate" : self.msgIn / timeDiff,
                 "outgoingRate" : (self.msgOut) / timeDiff,
        }


def statusStringToStatusCode(statusString):
    return { "rejectProducers": queue_status.STATUS_REJECTING_PRODUCERS,
             "rejectConsumers": queue_status.STATUS_REJECTING_CONSUMERS,
             "rejectAll"      : queue_status.STATUS_REJECTING_ALL,
             "resumeQueue"    : queue_status.STATUS_RUNNING,
    }[statusString]

def statusCodeToStatusString(statusCode):
    return { queue_status.STATUS_REJECTING_PRODUCERS: "rejecting producers",
             queue_status.STATUS_REJECTING_CONSUMERS: "rejecting consumers",
             queue_status.STATUS_REJECTING_ALL: "rejecting all",
             queue_status.STATUS_RUNNING: "running",
    }[statusCode]

